package com.qzyh.esb.egsa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qzyh.esb.egsa.bean.bo.Sm2Key;
import com.qzyh.esb.egsa.bean.dto.LoginRspDTO;
import com.qzyh.esb.egsa.bean.dto.PublicKeyDTO;
import com.qzyh.esb.egsa.bean.vo.LoginReqVO;
import com.qzyh.esb.egsa.config.JwtConfig;
import com.qzyh.esb.egsa.entity.User;
import com.qzyh.esb.egsa.mapper.UserMapper;
import com.qzyh.esb.egsa.service.ILoginService;
import com.qzyh.esb.egsa.singleton.SeqNoSingleton;
import com.qzyh.esb.egsa.utils.CollectUtil;
import com.qzyh.esb.egsa.utils.GmUtil;
import com.qzyh.esb.egsa.utils.ResultObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LoginServiceImpl implements ILoginService {

    private static final Logger logger = LoggerFactory.getLogger(LoginServiceImpl.class);

    @Value("${sm4Base64Str}")
    private String sm4Base64Str;


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtConfig jwtConfig;

    @Override
    public PublicKeyDTO genPublicKey() {
        //生成sm2密钥
        Sm2Key sm2Key = GmUtil.genSm2Key();
        //生成密钥索引
        String keyId = "Key" + SeqNoSingleton.getInstance().getSeqNo();
        //缓存私钥
        CollectUtil.put("private"+keyId,sm2Key.getPrivateKey());
        //缓存公钥
        CollectUtil.put("public"+keyId,sm2Key.getPublicKey());
        //返回密钥索引和公钥
        return new PublicKeyDTO(keyId,sm2Key.getPublicKey());
    }

    @Override
    public ResultObject<LoginRspDTO> login(LoginReqVO vo) {
        //获取登录名密文
        String loginNameCipher = vo.getLoginName();
        //获取登录密码密文
        String loginPwdCipher = vo.getLoginPwd();
        //密钥索引
        String keyId = vo.getKeyId();
        //获取密钥缓存
        Sm2Key sm2Key = new Sm2Key(CollectUtil.get("public"+keyId),CollectUtil.get("private"+keyId));

        //登录名解密成明文
        String loginName = GmUtil.sm2Decrypt(sm2Key,loginNameCipher);
        //登录密码解密成明文
        String loginPwd = GmUtil.sm2Decrypt(sm2Key,loginPwdCipher);

        //用 登录名+sm4加密后的密码 查询
        QueryWrapper<User> wrapper = Wrappers.query();
        wrapper.eq("login_name", loginName);
        wrapper.eq("login_pwd", GmUtil.sm4Encrypt(sm4Base64Str,loginPwd));
        List<User> users = userMapper.selectList(wrapper);
        if (users!=null && users.size()==1){//查询数据不为空且只有一条，登录成功
            User user = users.get(0);
            //生成token
            String token = jwtConfig.createToken(user.getId()) ;
            //获取角色id
            String roleId = user.getDefaultRoleId();
            //获取用户名
            String userName = user.getUserName();
            return ResultObject.success(new LoginRspDTO(token,roleId,userName));
        } else {//其余均为登录失败
            logger.info(loginName+"登录失败");
            return ResultObject.loginFail();
        }
    }
}
